package com.hustzp.com.xichuangzhu;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hustzp.com.xichuangzhu.poetry.BaiKeAct;
import com.hustzp.com.xichuangzhu.utils.ScreenshotUtil;
import com.hustzp.com.xichuangzhu.widget.NoScrollListView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/6/2 10:51
 * @Description:
 */
public class XCZBaseFragmentActivity extends FragmentActivity {
    private View cancelView;
    private NoScrollListView lvMenu;

    // 由底部进入 提示框view
    public View tipView;
    private Animation inAnimation;
    private Animation outAnimation;
    private Map<Integer, View> map = new HashMap<Integer, View>();// 存放提示框view
    private View currentView;
    private LinearLayout tooltipContent;
    private LinearLayout backContent;
    private View popListView;
    private View shareView;

    private LinearLayout popBox;
    private TextView popTitle;

    private UMShareAPI mShareAPI;
    private SHARE_MEDIA platform;

    public View screenShotView;
    public boolean isAdd = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mShareAPI = UMShareAPI.get(this);
    }

    public void initSharePopupWindow() {
        popListView = LayoutInflater.from(this).inflate(R.layout.popupwindow_layout, null);
        popBox = (LinearLayout) popListView.findViewById(R.id.pop_title_layout);
        popTitle = (TextView) popListView.findViewById(R.id.pop_list_title);
        lvMenu = (NoScrollListView) popListView.findViewById(R.id.lvMenu);
        cancelView = popListView.findViewById(R.id.cancel_id);
        cancelView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                hideAlert();
            }
        });
        addAlert(popListView);

        shareView = LayoutInflater.from(this).inflate(R.layout.share_popup_layout, null);
        addAlert(shareView);
    }

    public void share(View view) {
        switch (view.getId()) {
            case R.id.share_wechat_id:
                platform = SHARE_MEDIA.WEIXIN;
                break;
            case R.id.share_wechat_moment_id:
                platform = SHARE_MEDIA.WEIXIN_CIRCLE;
                break;
            case R.id.share_sina_id:
                platform = SHARE_MEDIA.SINA;
                break;
            case R.id.share_douban_id:
                platform = SHARE_MEDIA.DOUBAN;
                break;
        }

//        mShareAPI.doOauthVerify(this, platform, umAuthListener);

        new ShareAction(XCZBaseFragmentActivity.this)
                .setPlatform(platform)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onResult(SHARE_MEDIA platform) {
                        Toast.makeText(XCZBaseFragmentActivity.this, "分享成功啦", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA platform, Throwable t) {
                        t.printStackTrace();
                        Toast.makeText(XCZBaseFragmentActivity.this, "分享失败啦", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(XCZBaseFragmentActivity.this, "分享取消了", Toast.LENGTH_SHORT).show();
                    }
                })
                .withTitle("西窗烛APP")
                .withMedia(new UMImage(XCZBaseFragmentActivity.this, BitmapFactory.decodeFile(ScreenshotUtil.saveBitmapView(this, screenShotView, isAdd))))
                .share();
    }

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            new ShareAction(XCZBaseFragmentActivity.this)
                    .setPlatform(platform)
                    .setCallback(new UMShareListener() {
                        @Override
                        public void onResult(SHARE_MEDIA platform) {
                            Toast.makeText(XCZBaseFragmentActivity.this, "分享成功啦", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onError(SHARE_MEDIA platform, Throwable t) {
                            t.printStackTrace();
                            Toast.makeText(XCZBaseFragmentActivity.this, "分享失败啦", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA platform) {
                            Toast.makeText(XCZBaseFragmentActivity.this, "分享取消了", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .withText("西窗烛APP")
                    .withMedia(new UMImage(XCZBaseFragmentActivity.this, BitmapFactory.decodeResource(getResources(), R.mipmap.xcz_logo)))
                    .withTargetUrl("http://www.xichuangzhu.com/")
                    .share();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Log.d("SHARE_TAG_AUTH_FAIL", t.toString());
            t.printStackTrace();
            Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }

    public void showSharePopWindow() {
        showAlert(shareView.getId());
    }

    public void showSharePopupWindow() {
        showAlertAndHidePrior(shareView.getId());
    }

    public void showSharePopupWindow(List<String> menus, AdapterView.OnItemClickListener listener) {
        showSharePopupWindow(null, menus, listener);
    }

    public void showSharePopupWindow(String title, List<String> menus, AdapterView.OnItemClickListener listener) {
        if (title != null && !title.isEmpty()) {
            popTitle.setText(title);
            popBox.setVisibility(View.VISIBLE);
        } else {
            popBox.setVisibility(View.GONE);
        }

        bindData(menus, listener);
        showAlert(popListView.getId());
    }

    protected void addAlert(View v) {
        if (inAnimation == null) {
            inAnimation = AnimationUtils.loadAnimation(this, R.anim.window_in);
            outAnimation = AnimationUtils.loadAnimation(this, R.anim.window_out);
        }
        if (tipView == null) {
            tipView = View.inflate(this, R.layout.tooltip, null);
            ViewGroup contentView = (ViewGroup) this.getWindow().getDecorView().findViewById(android.R.id.content);
            contentView.addView(tipView);
        }
        if (tooltipContent == null) {
            tooltipContent = (LinearLayout) this.findViewById(R.id.ll_tooltip_content);
        }
        if (backContent == null) {
            backContent = (LinearLayout) this.findViewById(R.id.ll_back_content);
            backContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideAlert();
                }
            });
        }
        tooltipContent.addView(v);
        v.setVisibility(View.GONE);
        map.put(v.getId(), v);
    }

    public void hideAlert() {
        if (tipView != null && currentView != null) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    tipView.setVisibility(View.GONE);
                }
            }, 300);
            currentView.setVisibility(View.GONE);
            currentView.startAnimation(outAnimation);
        }
    }

    /***
     * @param id view.getId()
     */
    public void showAlert(int id) {
        currentView = map.get(id);
        if (tipView != null && currentView != null) {
            tipView.setVisibility(View.VISIBLE);
            tipView.setClickable(true);
            currentView.setVisibility(View.VISIBLE);
            currentView.startAnimation(inAnimation);
        }
    }

    public void showAlertAndHidePrior(final int id) {
        if (tipView != null && currentView != null) {
            Handler handler = new Handler();

            outAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    currentView.setVisibility(View.GONE);
                    outAnimation.setAnimationListener(null);
                    showAlert(id);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            currentView.startAnimation(outAnimation);
        }
    }

    public void bindData(List<String> menus, AdapterView.OnItemClickListener listener) {
        BaseAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.popupwindow_layout_item, R.id.tv_menu, menus);
        lvMenu.setAdapter(adapter);
        lvMenu.setOnItemClickListener(listener);
    }

    public void closeView(View view) {
        finish();
    }

    public void showToastInfo(String info) {
        Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
    }

    public void openBaike(String url) {
        Intent intent = new Intent(this, BaiKeAct.class);
        intent.putExtra(BaiKeAct.class.getSimpleName(), url);
        startActivity(intent);
    }
}
